package fr.mathis.tbbtmoments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import fr.mathis.tbbtmoments.dummy.DummyContent.DummyItem;

public class StateOlder {

	public static StateOlder instance;
	
	public boolean isTablet = false;
	public String query = "";
	
	public List<DummyItem> items_all = new ArrayList<DummyItem>();
	public Map<String, DummyItem> item_map_all = new HashMap<String, DummyItem>();
	
	public List<DummyItem> items_query = new ArrayList<DummyItem>();
	public Map<String, DummyItem> item_map_query = new HashMap<String, DummyItem>();
	
	public StateOlder()
	{
		isTablet = false;
		items_all = new ArrayList<DummyItem>();
		item_map_all = new HashMap<String, DummyItem>();
		items_query = new ArrayList<DummyItem>();
		item_map_query = new HashMap<String, DummyItem>();
		query = "";
	}

	public static StateOlder getInstance()
	{
		if(instance == null)
			instance = new StateOlder();
		return instance;		
	}
	
	public List<DummyItem> getItems()
	{
		if(query != null && query.compareTo("")!=0)
		{
			return items_query;
		}
		else {
			return items_all;
		}
	}
	
	public Map<String, DummyItem> getItemMap()
	{
		if(query != null && query.compareTo("")!=0)
		{
			return item_map_query;
		}
		else {
			return item_map_all;
		}
	}
	
	public void addItem(DummyItem item) {
		items_all.add(item);
		item_map_all.put(item.id, item);
	}
	
	@SuppressLint("DefaultLocale")
	public void setQuery(String newQuery)
	{
		query = newQuery;
		items_query = new ArrayList<DummyItem>();
		item_map_query = new HashMap<String, DummyItem>();
		for(DummyItem di : items_all)
		{
			if(di.episode.toLowerCase().contains(newQuery.toLowerCase()) || di.part.toLowerCase().contains(newQuery.toLowerCase()) || di.content.toLowerCase().contains(newQuery.toLowerCase()))
			{
				items_query.add(di);
				item_map_query.put(di.id, di);
			}
		}
	}
	
	public void removeSearch()
	{
		query = "";
		items_query = new ArrayList<DummyItem>();
		item_map_query = new HashMap<String, DummyItem>();
	}
}
